package com.hzpd.hainan.module.news.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzpd.hainan.R;
import com.hzpd.hainan.api.InterfaceJsonfile;
import com.hzpd.hainan.app.ToolBarActivity;
import com.hzpd.hainan.custorm.DonutProgress;
import com.hzpd.hainan.custorm.TouchImageView;
import com.hzpd.hainan.model.news.AlbumDetailEntity;
import com.hzpd.hainan.model.news.ImageListSubBean;
import com.hzpd.hainan.model.news.ImgListBean;
import com.hzpd.hainan.model.news.NewsBean;
import com.hzpd.hainan.model.news.NewsDetailBean;
import com.hzpd.hainan.model.usercenter.EmptyEntityList;
import com.hzpd.hainan.module.news.adapter.RecyclingPagerAdapter;
import com.hzpd.hainan.net.Factory;
import com.hzpd.hainan.utils.DeviceUtils;
import com.hzpd.hainan.utils.LogUtils;
import com.hzpd.hainan.utils.ParamUtils;
import com.hzpd.hainan.utils.TUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import me.relex.photodraweeview.PhotoDraweeView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by 猪猪侠 on 2017/7/5.
 */

public class NewsAlbumActivity extends ToolBarActivity implements View.OnClickListener {
    @BindView(R.id.img_detail_test_pager)
    ViewPager mViewPager;
    @BindView(R.id.img_detail_title_id)
    TextView mTextViewTitle;
    @BindView(R.id.img_detail_content_id)
    TextView mTextViewContent;
    @BindView(R.id.img_detial_number_id)
    TextView mTextViewNumber;
    @BindView(R.id.main_title_personal)
    LinearLayout mLayoutBack;
    @BindView(R.id.imgdetails_title_pl)
    LinearLayout imgdetails_title_pl;
    @BindView(R.id.album_donutProgress)
    DonutProgress donutProgress;
    private NewsAlbumAdapter simpleAdapter;

    private String from;//news album newsitem
    private int currentPosition = 0;

    @BindView(R.id.album_rl_head)
    RelativeLayout album_rl_head;//头部
    @BindView(R.id.album_rl_bottom)//底部
            RelativeLayout album_rl_bottom;

    private boolean animationFlag = false;//是否隐藏
    String tid;
    private NewsDetailBean ndb;
    private ImgListBean imgListBean;
    private ArrayList<String> images;
    NewsBean nb;
    @Override
    public String initToolBarTitle() {
        return null;
    }

    @Override
    public int setMyContentView() {
        return R.layout.news_detail_img_main_layout;
    }

    @Override
    public void initView() {
        setToolBarVisiable(false);
        String pid = null;
        Intent intent = getIntent();
        tid=intent.getStringExtra("tid");
        LogUtils.e("tid---"+tid);
        String action = intent.getAction();
        if (null != action && Intent.ACTION_VIEW.equals(action)) {
            Uri uri = intent.getData();
            if (uri != null) {
                pid = uri.getPath();
                pid = pid.replace(File.separator, "");
                from = "browser";
//                getAlbum_browser(pid);
            }
        } else {
            from = intent.getStringExtra("from");
        }

        initViewPage();

        if ("news".equals(from)) {
            images = intent.getStringArrayListExtra("imags");
            currentPosition = intent.getIntExtra("position", 0);
            ndb = (NewsDetailBean) intent.getSerializableExtra("ndb");

            List<ImageListSubBean> mlist = new ArrayList<ImageListSubBean>();
//			if(null!=ndb.getImgs()){
            for (String im : images) {
                ImageListSubBean ilsb = new ImageListSubBean("", im);
                mlist.add(ilsb);
            }
            simpleAdapter.setList(mlist);
//			}
            try {
                mTextViewTitle.setText(ndb.getTitle());
            } catch (Exception e) {

            }

            if (simpleAdapter.getCount() > currentPosition) {
                mViewPager.setCurrentItem(currentPosition);
            }
        } else if ("album".equals(from)) {
            currentPosition = 0;
            imgListBean = (ImgListBean) intent.getSerializableExtra("imgbean");
            if (null != imgListBean) {
                simpleAdapter.setList(imgListBean.getSubphoto());
            }
        } else if ("collection".equals(from)) {
            getAlbum_ni(intent.getStringExtra("pid"));
        } else if ("newsitem".equals(from)) {
            //获取图集
            LogUtils.i("newbean");
            nb= (NewsBean) intent.getSerializableExtra("newbean");
            if (null != nb.getRvalue() && !"".equals(nb.getRvalue())) {
                getAlbum_ni(nb.getRvalue());
            }
            //获取图集
        } else if ("browser".equals(from)) {
            LogUtils.i("browser");
//            getAlbum_browser(pid);
        } else {
            return;
        }

        mLayoutBack.setOnClickListener(this);

        imgdetails_title_pl.setOnClickListener(this);
//        addVisits();
    }

    private void initViewPage() {
        simpleAdapter = new NewsAlbumAdapter();

//        mViewPager.setPageMargin(10);

        mViewPager.setAdapter(simpleAdapter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int arg0) {

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageSelected(int arg0) {
                mTextViewNumber.setText((arg0 + 1) + "/" + simpleAdapter.getCount());
                mTextViewContent.setText(simpleAdapter.getDes(arg0));
                currentPosition = arg0;
            }
        });
        mViewPager.setCurrentItem(currentPosition);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.main_title_personal: {
                finish();
            }
            break;
            case R.id.imgdetails_title_pl: {
                //分享 收藏  写评论
//                popUpwindow();
            }
            break;
        }
    }
    public  void addVisits(){

        Map<String,String> map=new HashMap<>();
        map.put("nid",nb.getRvalue());
        map.put("type","2");
        map.put("siteid", InterfaceJsonfile.SITEID);
        map.put("num","1");
        map.put("tid",tid);
        if (spu.getUser()!=null){
            map.put("uid",spu.getUser().getUid());
        }

        map.put("device", DeviceUtils.getMyUUID(activity));
        Factory.provideHttpService().addvisit(ParamUtils.getRequestParam(map))
                .onBackpressureBuffer()
                .subscribeOn(Schedulers.io())
                .filter(new Func1<EmptyEntityList, Boolean>() {
                    @Override
                    public Boolean call(EmptyEntityList emptyEntityList) {
                        return true;
                    }
                })

                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<EmptyEntityList>() {
                    @Override
                    public void call(EmptyEntityList emptyEntityList) {
                        LogUtils.i("emptyEntityList----"+emptyEntityList.code);
                    }

                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

    class NewsAlbumAdapter extends RecyclingPagerAdapter {
//        @BindBitmap(R.id.pdv_img)
//        PhotoDraweeView pdvImg;
        private List<ImageListSubBean> list;
        private LayoutInflater inflater;

        public NewsAlbumAdapter() {

            list = new ArrayList<ImageListSubBean>();
            inflater = LayoutInflater.from(NewsAlbumActivity.this);

        }

        public ImageListSubBean getCurrentBean(int position) {
            return list.get(position % list.size());
        }

        public void setList(List<ImageListSubBean> mList) {
            if (mList.size() > 0) {
                clearList();
                addList(mList);
            }
        }

        public int getListSize() {
            return list.size();
        }

        public void addList(List<ImageListSubBean> mList) {
            list.addAll(mList);
            mTextViewNumber.setText("1/" + list.size());

            if ("news".equals(from)) {
//				imgdetails_title_pl.setVisibility(View.GONE);
//					mTextViewTitle.setText(ndb.getTitle());

            } else {
                try {
                    mTextViewTitle.setText(ndb.getTitle());
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            if (list.size() > 0) {
                mTextViewContent.setText(list.get(0).getSubdesc());
            }
            notifyDataSetChanged();
        }

        public void clearList() {
            list.clear();
        }

        @Override
        public int getCount() {
            return list.size();
        }

        public String getDes(int positon) {
            return list.get(positon).getSubdesc();
        }


        @Override
        public View getView(int position, View convertView, ViewGroup container) {
//            View view = inflater.inflate(R.layout.album_item, container, false);
            TouchImageView imgview = new TouchImageView(activity);
//            imgview = (TouchImageView)view.findViewById(R.id.pdv_img);
            Glide.with(activity).load(list.get(position).getSubphoto()).crossFade().placeholder(R.drawable.default_bg).into(imgview);
            return imgview;
        }

    }

    //获取单个图集
    private void getAlbum_ni(String nid) {
        Map<String, String> map = new HashMap<>();
        map.put("pid", nid);
        LogUtils.i("pid-->" + nid);
        Factory.provideHttpService()
                .albumDetail(map)
                .subscribeOn(Schedulers.io())
                .filter(new Func1<AlbumDetailEntity, Boolean>() {
                    @Override
                    public Boolean call(AlbumDetailEntity entity) {
                        if ("200".equals(entity.code)) {
                            return true;
                        }
                        return false;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<AlbumDetailEntity>() {
                    @Override
                    public void call(AlbumDetailEntity entity) {
                        currentPosition = 0;
                        simpleAdapter.setList(entity.data.getSubphoto());
                        addVisits();
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("throwable-->" + throwable.toString());
                        TUtils.toast("获取图集失败");
                        finish();
                    }
                });
    }

    @Override
    public void initData() {
    }
}
